using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFGameFramework;
using XFGameFramework.ArchiveSystem;
using XFGameFramework.BuffSystem;

public class GameSaveController : Controller
{
    GameController gameController => Module.LoadController<GameController>();
    TimeController timeController => Module.LoadController<TimeController>();
    PlayerController playerController => Module.LoadController<PlayerController>();
    ///`1
    /// <summary>
    /// 创建全局存档
    public GameModel GetGlobalSave()
    {
        if (ArchiveManager.Get<GameModel>(101) == null)
        {
            ArchiveManager.Create<GameModel>(101);
        }
        return ArchiveManager.Get<GameModel>(101);
    }
    public void SaveGlobalSave()
    {
        ArchiveManager.Save(101);
    }

    /// 创建游戏存档
    /// </summary>
    /// <param name="index"></param>
    public void CreateGameSave(int index)
    {

        TimeModel timeModel = ArchiveManager.Create<TimeModel>(index + 5);
        PlayerModel playerModel = ArchiveManager.Create<PlayerModel>(index);
        if (index == 0)//每次创建0号存档必然意味着0号存档被清除，需要重新引用
        {
            timeController.Reload();
            playerController.Reload();
        }
        // GameSaveModel gameSaveModel = ArchiveManager.Create<GameSaveModel>(index);

    }
    public TimeModel GetTimeModel(int index)
    {
        if (ArchiveManager.Get<TimeModel>(index + 5) != null)
        {
            return ArchiveManager.Get<TimeModel>(index + 5);
        }
        else //(ArchiveManager.Get<GameSaveModel>(index) == null)
        { return null; }
    }
    public PlayerModel GetPlayerModel(int index)
    {
        if (ArchiveManager.Get<PlayerModel>(index) != null)
        {
            return ArchiveManager.Get<PlayerModel>(index);

        }
        else //(ArchiveManager.Get<PlayerModel>(index) == null)
        { return null; }
    }

    /// <summary>
    /// 保存游戏存档
    /// </summary>
    /// <param name="index"></param>
    public void SaveGameSave(int index)
    {
        ArchiveManager.Save(index + 5);
        ArchiveManager.Save(index);
        Debug.Log($"保存存档{index}");
    }

    /// <summary>
    /// 删除游戏存档
    /// </summary>
    /// <param name="index"></param>
    public void DeleteGameSave(int index)
    {
        ArchiveManager.Delete(index);
        ArchiveManager.Delete(index + 5);

    }
    public bool GetExists()
    {
        for (int i = 1; i < 5; i++)
        {
            if (GetTimeModel(i) != null) return true;
        }
        return false;
    }
    public bool GetExists(int index)
    {
        if (GetTimeModel(index) != null) return true;
        return false;
    }
    public int GetArchiveCount()
    {
        return ArchiveManager.AllIds.Count / 2;
    }


    /// <summary>
    /// 覆盖游戏存档
    /// </summary>
    /// <param name="newindex"></param>x新存档的索引
    /// <param name="oldindex"></param>当前存档号
    public void CoverGameSave(int newindex, int oldindex = 0)
    {

        TimeModel oldTimeModel = GetTimeModel(oldindex);
        PlayerModel oldPlayerModel = GetPlayerModel(oldindex);

        //只获取数值而不获取引用
        //用temp变量保存旧存档值
        Period currentPeriod = oldTimeModel.currentPeriod;
        int days = oldTimeModel.days;
        int hours = oldTimeModel.hours;
        int minutes = oldTimeModel.minutes;
        int remainingTime = oldTimeModel.remainingTime;

        int Hp = oldPlayerModel.Hp;
        int Endurance = oldPlayerModel.Endurance;

        //保存旧存档使得旧存档获得新数据，创建新存档

        DeleteGameSave(newindex);
        CreateGameSave(newindex);
        oldTimeModel = GetTimeModel(oldindex);
        oldPlayerModel = GetPlayerModel(oldindex);

        TimeModel newTimeModel = GetTimeModel(newindex);
        PlayerModel newPlayerModel = GetPlayerModel(newindex);
        //新存档获取已保存存档数据
        newTimeModel.currentPeriod = oldTimeModel.currentPeriod;
        newTimeModel.days = oldTimeModel.days;
        newTimeModel.hours = oldTimeModel.hours;
        newTimeModel.minutes = oldTimeModel.minutes;
        newTimeModel.remainingTime = oldTimeModel.remainingTime;

        newPlayerModel.Hp = Hp;
        newPlayerModel.Endurance = Endurance;
        SaveGameSave(newindex);

        GameController gameController = Module.LoadController<GameController>();
        gameController.SetArchiveIndex(newindex);
        Debug.Log($"当前存档已被设置为{newindex}");

        //旧存档获取到最初值
        oldTimeModel.currentPeriod = currentPeriod;
        oldTimeModel.days = days;
        oldTimeModel.hours = hours;
        oldTimeModel.minutes = minutes;
        oldTimeModel.remainingTime = remainingTime;
        oldPlayerModel.Hp = Hp;
        oldPlayerModel.Endurance = Endurance;
    }
}       
